package org.study.neo4j.boot.util;

import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.study.neo4j.boot.dto.*;
import org.study.neo4j.boot.model.*;

import java.util.stream.Collectors;

/**
 * 作用描述
 *
 * @author doveylovey
 * @version v1.0.0
 * @email 1135782208@qq.com
 * @date 2025年04月09日
 */
@Slf4j
@UtilityClass
public class ConvertToDtoUtils {
    public StudentDTO convertToStudentDTO(Student student) {
        StudentDTO dto = new StudentDTO();
        dto.setId(student.getId());
        dto.setName(student.getName());
        dto.setAge(student.getAge());
        dto.setGrade(student.getGrade());
        if (CollectionUtils.isNotEmpty(student.getCourses())) {
            dto.setCourses(student.getCourses()
                    .stream()
                    .map(course -> {
                        StudentDTO.CourseSimpleDTO courseDTO = new StudentDTO.CourseSimpleDTO();
                        courseDTO.setId(course.getId());
                        courseDTO.setCode(course.getCode());
                        courseDTO.setTitle(course.getTitle());
                        return courseDTO;
                    })
                    .collect(Collectors.toSet())
            );
        }
        if (CollectionUtils.isNotEmpty(student.getTeachers())) {
            dto.setTeachers(student.getTeachers()
                    .stream()
                    .map(teacher -> {
                        StudentDTO.TeacherSimpleDTO teacherDTO = new StudentDTO.TeacherSimpleDTO();
                        teacherDTO.setId(teacher.getId());
                        teacherDTO.setName(teacher.getName());
                        teacherDTO.setDepartment(teacher.getDepartment());
                        return teacherDTO;
                    })
                    .collect(Collectors.toSet())
            );
        }
        return dto;
    }

    public TeacherDTO convertToTeacherDTO(Teacher teacher) {
        TeacherDTO dto = new TeacherDTO();
        dto.setId(teacher.getId());
        dto.setName(teacher.getName());
        dto.setDepartment(teacher.getDepartment());
        dto.setEmail(teacher.getEmail());
        if (CollectionUtils.isNotEmpty(teacher.getCourses())) {
            dto.setCourses(teacher.getCourses()
                    .stream()
                    .map(course -> {
                        TeacherDTO.CourseSimpleDTO courseDTO = new TeacherDTO.CourseSimpleDTO();
                        courseDTO.setId(course.getId());
                        courseDTO.setCode(course.getCode());
                        courseDTO.setTitle(course.getTitle());
                        return courseDTO;
                    })
                    .collect(Collectors.toSet())
            );
        }
        if (CollectionUtils.isNotEmpty(teacher.getStudents())) {
            dto.setStudents(teacher.getStudents()
                    .stream()
                    .map(student -> {
                        TeacherDTO.StudentSimpleDTO studentDTO = new TeacherDTO.StudentSimpleDTO();
                        studentDTO.setId(student.getId());
                        studentDTO.setName(student.getName());
                        studentDTO.setGrade(student.getGrade());
                        return studentDTO;
                    })
                    .collect(Collectors.toSet())
            );
        }
        return dto;
    }

    public CourseDTO convertToCourseDTO(Course course) {
        CourseDTO dto = new CourseDTO();
        dto.setId(course.getId());
        dto.setCode(course.getCode());
        dto.setTitle(course.getTitle());
        dto.setCredits(course.getCredits());
        if (course.getTeacher() != null) {
            CourseDTO.TeacherSimpleDTO teacherDTO = new CourseDTO.TeacherSimpleDTO();
            teacherDTO.setId(course.getTeacher().getId());
            teacherDTO.setName(course.getTeacher().getName());
            dto.setTeacher(teacherDTO);
        }
        if (CollectionUtils.isNotEmpty(course.getStudents())) {
            dto.setStudents(course.getStudents()
                    .stream()
                    .map(student -> {
                        CourseDTO.StudentSimpleDTO studentDTO = new CourseDTO.StudentSimpleDTO();
                        studentDTO.setId(student.getId());
                        studentDTO.setName(student.getName());
                        return studentDTO;
                    })
                    .collect(Collectors.toSet())
            );
        }
        return dto;
    }
}
